<?php

namespace App\Model;

use App\Model\Member;
use Illuminate\Database\Eloquent\Model;

class Recommend extends Model
{
    public $timestamps = false;

    protected $table = 'recommend';

    protected $primaryKey = 'recommendID';

    protected $fillable = [
        'recommendID', //id
        'userID', //推荐人ID
        'buserID', //被推荐人ID
        'cuserID',  //推荐人的上一级推荐人ID 备注：
                    //1.代理商推荐代理商或商家，此处填写推荐人的推荐人id，如果为原点存-1
                    //2.商家推荐商家或会员，此处填写推荐人的代理商id
                    //3.会员推荐会员，此处填写推荐人的推荐人商家id
                    //4.代理商推荐代理商时，此处填写上级推荐人id，其他填写上级人id
                    //5.-1表明为原点推荐
        'type', //推荐类型 备注：
                //1.）如果为代理商，则为1
                //2.）如果为商家，则为2
                //3.）如果为会员，则为3
                //4.）默认为0

        'time', //时间
    ];

    //与推荐人关联
    public function member()
    {
        return $this->hasOne('App\\Models\\User', 'userID', 'userID');
    }

    //与被推荐人关联
    public function buser()
    {
        return $this->hasOne('App\\Models\\User', 'userID', 'buserID');
    }

    //与推荐人上一级推荐人关联
    public function cuser()
    {
        return $this->hasOne('App\\Models\\User', 'userID', 'cuserID');
    }

    /**
     * 返回即将写入推荐线的数据
     *
     * @author: zuorenci
     * @date: 2017年11月6日
     * @access public
     * @param  int $userid       要入推荐线的人员id
     * @return array
     */
    public function inRecommend_new($userid)
    {
        if (empty($userid)) {
            return false;
        }
        //查询userid是否已进入推荐线
        $user_recommend = $this->where(['buserID' => $userid])->first();
        if ($user_recommend) {
            //该商家已经入推荐线
            return false;
        }
        //获取userid的用户信息
        $user_msg = Member::where(['userID' => $userid])->first();
        if (empty($user_msg)) {
            return false;
        }
        if ($user_msg['PID'] == 0) {
            //无推荐人
            return false;
        } else {
            //有推荐人
            //$field = 'userID,PID,type';
            //查询推荐人是否存在
            $user_pmsg = Member::where(['userID' => $user_msg['PID']])->first();
            if (empty($user_pmsg)) {
                //推荐人不存在
                return false;
            }
            //判断推荐人的身份
            if ($user_pmsg['PID'] == -1) {
                //推荐人为原点
                $data['cuserID'] = $user_msg['PID'];
                $data['userID']  = $user_msg['PID']; //by-yyy20170801修改
            } elseif ($user_pmsg['PID'] == 0) {
                //无推荐人
                return false;
            } else {
                if ($user_pmsg['type'] == 1) {
                    //推荐人为代理商
                    $data['cuserID'] = $user_pmsg['userID'];
                } elseif ($user_pmsg['type'] == 2) {
                    //推荐人为商家
                    $data['cuserID'] = $this->where(['buserID' => $user_pmsg['userID']])->value('cuserID');
                }
                $data['userID'] = $user_pmsg['userID'];
            }
        }

        //要写入recommend表中的数据
        $data['buserID'] = $userid;
        $data['type']    = 2;
        $data['time']    = time();
        return $data;
    }

    /**
     * 商家管理收益静态化关系存贮(商家)（返回数据）
     * @author: zuorenci
     * @date: 2017/11/6
     * @param $result
     * @return array
     */
    public function add_shop_referrer_new($result)
    {
        if ($result) {
            $data1 = [
                'userID'  => $result['buserID'],
                'auserID' => $result['userID'],
                'type'    => 5,
                'time'    => time(),
            ];
            return ['status' => 1, 'data' => [$data1]];
        } else {
            $return['status'] = 0;
            $return['msg']    = 2;
            return $return;
        }
    }

    /**
     * 会员管理收益静态化关系存贮(商家)（返回数据）
     * @author: zuorenci
     * @date: 2017/11/7
     * @param array $result
     * @return array
     */
    public function add_shop_manage_new($result)
    {
        if ($result) {
            $userid = $result['buserID'];
            if ($result['userID'] == $result['cuserID']) {
                //3为代理
                $result1 = $this->get_recommend($result['userID']);
                if ($result1) {
                    if ($result1['userID'] == -1) {
                        //3为原点
                        $data1 = [
                            ['userID' => $userid, 'auserID' => $result['userID'], 'type' => 3, 'time' => time()],
                            ['userID' => $userid, 'auserID' => 0, 'type' => 4, 'time' => time()],
                        ];
                    } else {
                        //3为创客
                        $data1 = [
                            ['userID' => $userid, 'auserID' => $result['userID'], 'type' => 3, 'time' => time()],
                            ['userID' => $userid, 'auserID' => $result1['userID'], 'type' => 4, 'time' => time()],
                        ];
                    }
                    return ['status' => 1, 'data' => $data1];
                } else {
                    $return['status'] = 0;
                    $return['msg']    = 3;
                    return $return;
                }
            } else {
                //3为商家
                $data1 = [
                    ['userID' => $userid, 'auserID' => $result['userID'], 'type' => 3, 'time' => time()],
                    ['userID' => $userid, 'auserID' => $result['cuserID'], 'type' => 4, 'time' => time()],
                ];
            }
            return ['status' => 1, 'data' => $data1];
        } else {
            $return['status'] = 0;
            $return['msg']    = 2;
            return $return;
        }
    }
    /**
     * 获取用户推荐线信息
     * @ author: zuorenci
     * @ date:2017/11/7 20:22
     * @ param $userid 用户id
     * @ return array|false|\PDOStatement|string|\think\Model
     */
    public function get_recommend($userid)
    {
        if (empty($userid)) {
            return false;
        }
        $map['buserID'] = $userid;
        $recommend      = $this->where($map)->first();
        return $recommend;
    }
}
